package tech.aistar.day11.homework;

import java.util.*;

/**
 * 本类用来演示:
 *  "abdfdfsadfdsfasadfdd" -> 统计每个字母出现的次数
 *
 *  "java is good the python js  is good the python" - 统计每个单词出现的次数
 *
 *  1 2 3 1 2 4 2 1 3 2 5 6 - 统计数字出现的次数
 *
 * @author: success
 * @date: 2021/3/23 8:43 上午
 */
public class TestPurcase {
    public static void main(String[] args) {
        List<Purcase> list = new ArrayList<>();

        Purcase p1 = new Purcase("宝洁","洗手粉",18.5);
        Purcase p2 = new Purcase("联合利华","肥皂",4.5);
        Purcase p3 = new Purcase("宝洁","牙膏",32.5);
        Purcase p4 = new Purcase("宝洁","毛巾",14.5);
        Purcase p5 = new Purcase("洁利","洗面奶",26.0);
        Purcase p6 = new Purcase("好迪","洗发水",27.5);
        Purcase p7 = new Purcase("多芬","沐浴露",38.5);
        Purcase p8 = new Purcase("宝洁","洗洁精",3.4);

        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        list.add(p6);
        list.add(p7);
        list.add(p8);

        countAndSort(list);
    }

    //借助于第三方实体类封装分类统计的结果 - 排序
    private static void countSort(List<Purcase> list) {
        //分类
        //目的:  品牌       对象
        //      宝洁     p1,p3,p4,p8 - list集合
        Map<String,List<Purcase>> maps = new HashMap<>();

        //遍历list
        for (Purcase purcase : list) {
            //第一次进来...
            //获取品牌名 - key
            String brand = purcase.getBrand();
            //判断maps中是否包含key - brand
            if(maps.containsKey(brand)){
                //根据key获取value
                List<Purcase> ps = maps.get(brand);
                //将当前的purcase放入到ps集合中
                ps.add(purcase);
            }else{
                //每次遇到一个新的品牌,那么需要创建一个List<Purcase>对象
                List<Purcase> pList = new ArrayList<>();
                pList.add(purcase);
                //放入到maps集合中
                maps.put(brand,pList);
            }
        }

        //创建一个集合,为排序做准备
        List<PurcaseVo> vos = new ArrayList<>();

        //遍历maps,统计每个品牌对应的总价
        Set<String> keySet = maps.keySet();
        Iterator<String> iter = keySet.iterator();
        while(iter.hasNext()){
            String brand = iter.next();//key - 品牌名
            //定义一个变量 - 用来统计这个品牌的总价
            double total = 0.0d;

            //获取这个品牌对应的所有的购买对象
            List<Purcase> purList = maps.get(brand);
            //遍历...
            for (Purcase purcase : purList) {
                total+=purcase.getCost();
            }

            //System.out.println(brand+":"+total);
            //使用第三方实体类来封装统计得到的数据
            PurcaseVo vo = new PurcaseVo(brand,total);
            vos.add(vo);//放入到集合
        }

        //排序...
        vos.sort((v1,v2)->(int)(v2.getTotal()-v1.getTotal()));

        //输出
        for (PurcaseVo vo : vos) {
            System.out.println(vo.getBrand()+":"+vo.getTotal());
        }
    }

    public static void countAndSort(List<Purcase> list){
        //分类
        //目的:  品牌       对象
        //      宝洁     p1,p3,p4,p8 - list集合
        Map<String,List<Purcase>> maps = new HashMap<>();

        //遍历list
        for (Purcase purcase : list) {
            //第一次进来...
            //获取品牌名 - key
            String brand = purcase.getBrand();
            //判断maps中是否包含key - brand
            if(maps.containsKey(brand)){
                //根据key获取value
                List<Purcase> ps = maps.get(brand);
                //将当前的purcase放入到ps集合中
                ps.add(purcase);
            }else{
                //每次遇到一个新的品牌,那么需要创建一个List<Purcase>对象
                List<Purcase> pList = new ArrayList<>();
                pList.add(purcase);
                //放入到maps集合中
                maps.put(brand,pList);
            }
        }

        //Map特性 - 封装统计之后的数据品牌 - 总价
        Map<String,Double> countMap = new HashMap<>();

        //遍历maps,统计每个品牌对应的总价
        Set<String> keySet = maps.keySet();
        Iterator<String> iter = keySet.iterator();
        while(iter.hasNext()){
            String brand = iter.next();//key - 品牌名
            //定义一个变量 - 用来统计这个品牌的总价
            double total = 0.0d;

            //获取这个品牌对应的所有的购买对象
            List<Purcase> purList = maps.get(brand);
            //遍历...
            for (Purcase purcase : purList) {
                total+=purcase.getCost();
            }

            countMap.put(brand,total);
        }

        //创建一个List - 排序进行准备
        List<Map.Entry<String,Double>> sortList = new ArrayList<>();

        //技巧 - 充分使用到了Map第二种迭代器
        //就是将countMap中的key,value封装到了内置对象中Entry
        Set<Map.Entry<String,Double>> entrySet = countMap.entrySet();
        Iterator<Map.Entry<String,Double>> iterSet = entrySet.iterator();
        while(iterSet.hasNext()){
            Map.Entry<String,Double> entry = iterSet.next();
            sortList.add(entry);
        }

        sortList.sort((e1,e2)->(int)(e2.getValue()-e1.getValue()));

        for (Map.Entry<String, Double> e : sortList) {
            System.out.println(e.getKey()+":"+e.getValue());
        }
    }
}
